package usercenter

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
	"time"
)

var _ UserFeedbackModel = (*customUserFeedbackModel)(nil)

type (
	// UserFeedbackModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserFeedbackModel.
	UserFeedbackModel interface {
		userFeedbackModel
		Find(ctx context.Context, uid int64, state int64, start, end *time.Time, pageNo, pageSize int64) ([]*UserFeedback, error)
		Count(ctx context.Context, uid int64, state int64, start, end *time.Time) (int64, error)
		Reply(ctx context.Context, id int64, adminUid int64, state int64, reply string, replyAttach string) error
	}

	customUserFeedbackModel struct {
		*defaultUserFeedbackModel
		genSql *gensql.GenSql
	}
)

// NewUserFeedbackModel returns a model for the database table.
func NewUserFeedbackModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserFeedbackModel {
	return &customUserFeedbackModel{
		defaultUserFeedbackModel: newUserFeedbackModel(conn, c, opts...),
		genSql:                   gensql.NewGenSql(),
	}
}

func (m *customUserFeedbackModel) Find(ctx context.Context, uid int64, state int64, start, end *time.Time, pageNo, pageSize int64) ([]*UserFeedback, error) {
	q := fmt.Sprintf("select %s from %s ", userFeedbackRows, m.table)
	m.genSql.InitSql(q)
	if uid > 0 {
		m.genSql = m.genSql.Where("uid = ?", uid)
	}
	if start != nil {
		m.genSql = m.genSql.Where("create_time >= ?", start)
	}
	if end != nil {
		m.genSql = m.genSql.Where("create_time <= ?", end)
	}
	if state > 0 {
		m.genSql = m.genSql.Where("state = ?", state)
	}

	// 分页
	if pageNo < 1 {
		pageNo = 1
	}
	if pageSize <= 0 {
		pageSize = 10
	}

	m.genSql = m.genSql.OrderBy("create_time desc").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize))

	query, values, err := m.genSql.ToSql()
	if err != nil {
		return nil, err
	}
	resp := make([]*UserFeedback, 0)
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

func (m *customUserFeedbackModel) Count(ctx context.Context, uid int64, state int64, start, end *time.Time) (int64, error) {
	q := fmt.Sprintf("select count(id) from %s ", m.table)
	m.genSql.InitSql(q)
	if uid > 0 {
		m.genSql = m.genSql.Where("uid = ?", uid)
	}
	if start != nil {
		m.genSql = m.genSql.Where("create_time >= ?", start)
	}
	if end != nil {
		m.genSql = m.genSql.Where("create_time <= ?", end)
	}
	if state > 0 {
		m.genSql = m.genSql.Where("state = ?", state)
	}
	query, values, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}

func (m *customUserFeedbackModel) Reply(ctx context.Context, id int64, adminUid int64, state int64, reply string, replyAttach string) error {
	query := fmt.Sprintf("update %s set reply = ?, reply_time = ?, admin_uid = ?, state = ?, reply_attachments = ? where id = ?", m.table)
	_, err := m.ExecNoCacheCtx(ctx, query, reply, time.Now(), adminUid, state, replyAttach, id)
	return err
}
